package com.imagination.applet.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.imagination.applet.controller.api.enums.TrainingStatusEnum;
import com.imagination.applet.domain.adapter.facechain.FaceChainAdapter;
import com.imagination.applet.domain.adapter.facechain.impl.BaseFaceChainAdapter;
import com.imagination.applet.domain.product.model.ProductImage;
import com.imagination.applet.domain.product.repository.ProductImageRepository;
import com.imagination.applet.domain.user.model.User;
import com.imagination.applet.domain.user.repository.UserRepository;
import com.imagination.applet.domain.user.service.UserService;
import com.imagination.applet.domain.virtualidentity.model.UserVirtualIdentity;
import com.imagination.applet.domain.virtualidentity.repository.UserVirtualIdentityRepository;
import com.imagination.applet.infrastructure.handler.ServiceException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VirtualIdentityTask {

    @Resource
    private UserVirtualIdentityRepository userVirtualIdentityRepository;
    @Resource
    private ProductImageRepository productImageRepository;
    @Resource
    private FaceChainAdapter faceChainAdapter;
    @Resource
    private UserService userService;
    @Resource
    private UserRepository userRepository;

//    @Scheduled(fixedRate = 5, timeUnit = TimeUnit.SECONDS)
    public void virtualIdentity() {
        List<UserVirtualIdentity> userVirtualIdentities = userVirtualIdentityRepository.queryByTrainingStatusAndDeleted(TrainingStatusEnum.TRAINING_RUNNING.getCode(), 0);
        if (userVirtualIdentities.isEmpty()) {
            return;
        }
        userVirtualIdentities.forEach(virtualIdentity -> {
            Long id = virtualIdentity.getId();
            Integer trainingStatus = faceChainAdapter.trainingStatus(id);
            if (trainingStatus != 0) {
                if (trainingStatus == 1) {
                    virtualIdentity.setTrainingStatus(1);
                }
                if(trainingStatus == -1) {
                    virtualIdentity.setTrainingStatus(-1);
                }
            }
        });
        userVirtualIdentityRepository.saveAll(userVirtualIdentities);
    }

//    @Scheduled(fixedRate = 5, timeUnit = TimeUnit.SECONDS)
    public void productionImage() {
//        List<ProductImage> productImageList = productImageRepository.getByTrainingStatus(TrainingStatusEnum.TRAINING_RUNNING.getCode());
//        if (productImageList.isEmpty()) {
//            return;
//        }
//        productImageList.forEach(productImage -> {
//            String taskId = productImage.getTaskId();
//            Long userId = productImage.getUserId();
//            JSONObject aiRsp = faceChainAdapter.inferenceStatus(taskId);
//            if (aiRsp.getInteger("code") == 0) {
//                if(aiRsp.getString("message").contains("running")){
//                    productImage.setTrainingStatus(TrainingStatusEnum.TRAINING_RUNNING.getCode());
//                }else{
//                    productImage.setTrainingStatus(1);
//                    productImage.setImagePath(aiRsp.getJSONArray("data").get(0).toString());
//                }
//            } else if (aiRsp.getInteger("code") == 2) {
//
//            }else{
//                    productImage.setTrainingStatus(-1);
//                    productImage.setDeleted(1);
//                    Optional<User> user = userService.getByUserId(userId);
//                    // 账户消费星星
//                    if(user.isPresent()){
//                        User update = user.get();
//                        int start = update.getStarCount() + 6;
//                        update.setStarCount(start);
//                        userRepository.saveAndFlush(update);
//                    }
//                }
//        });
//        productImageRepository.saveAll(productImageList);
    }
}
